package org.wjw.sys.controller;
import org.wjw.sys.service.IDictionaryService;
import org.wjw.sys.domain.Dictionary;
import org.wjw.sys.query.DictionaryQuery;
//11111111
import org.wjw.base.util.AjaxResult;
import org.wjw.dto.accept.ResultDTO;
//11111111
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dictionary")
public class DictionaryController {
    @Autowired
    public IDictionaryService dictionaryService;


    /**
     * 保存和修改公用的
     * @param dictionary  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Dictionary dictionary){
        try {
            if( dictionary.getId()!=null)
                dictionaryService.update(dictionary);
            else
                dictionaryService.insert(dictionary);
            return AjaxResult.getself();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getself().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            dictionaryService.deleteById(id);
            return AjaxResult.getself();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.getself().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id")Long id)
    {
        try {
            Dictionary dictionary = dictionaryService.selectById(id);
            return AjaxResult.getself().setObj(dictionary);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getself().setSuccess(false).setMessage("获取一个失败！"+e.getMessage());
        }
    }


    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            dictionaryService.batchDelete(ids);
            return AjaxResult.getself();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("删除出错"+e.getMessage());
        }
    }

    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult list(){

        try {
            List< Dictionary> list = dictionaryService.getAll();
            return AjaxResult.getself().setObj(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getself().setSuccess(false).setMessage("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult json(@RequestBody DictionaryQuery query)
    {
        try {
            ResultDTO<Dictionary> pageList = dictionaryService.getAllPage(query);
            return AjaxResult.getself().setObj(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getself().setSuccess(false).setMessage("获取分页数据失败！"+e.getMessage());
        }
    }
}
